<!-- 打包复核 -->
<template>
	<view>
		<scan></scan>
		<scanner></scanner>
		<sm></sm>
		<honeywell></honeywell>
		
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" >
				<u-row >
					<u-col  span="4">
						<u-input v-model="createDate" type="select" placeholder="请选择导入时间" @click="getT2()" clearable/>
					</u-col>
					<u-col  span="8">
						<u-search placeholder="请输入快递单号搜索" :clearabled="true" v-model="keyword" border-color="#606266" @focus="getKeyWordFocus" @custom="search" @search="search"></u-search>
					</u-col>
				</u-row>
				
				
			</view>
		</u-sticky>
		
		
		<u-card padding="16" v-for="(item,index) in dataList" :key="index" @click="toPackReview(item)">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="7">
							<view class="u-m-t-5 weikaishi3">
								<span><u-icon name="grid" size="30" labelSize="15px" labelColor="#000000"></u-icon>{{item.orderType}}</span>
							</view>
						</u-col>
						<u-col span="5">
							<view class="u-m-t-5 jinxingzhong weikaishi3">
								<span>状态：</span><span>{{item.reviewStatus}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row >
						<u-col span="12">
							<view class="weikaishi3">
								<span><u-icon name="order" size="29" labelSize="14px" labelColor="#000000" label="快递单号" color="#000000"></u-icon>：{{item.trackNumber}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row >
						<u-col span="12">
							<view class="weikaishi3">
								<span><u-icon name="order" size="29" labelSize="14px" labelColor="#000000" label="拣选单号" color="#000000"></u-icon>：{{item.pickNumber}}</span>
							</view>
						</u-col>
					</u-row>
					
					<u-row >
						<u-col span="12">
							<view class="weikaishi3">
								<span><u-icon name="calendar" size="29" labelSize="14px" labelColor="#000000" label="导入时间"></u-icon>：{{item.createTime}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row >
						<u-col span="12">
							<view class="weikaishi3">
								<span><u-icon name="car" size="29" labelSize="14px" labelColor="#000000" label="下单时间"></u-icon>：{{item.sendOutTime}}</span>
							</view>
						</u-col>
					</u-row>
			
				</view>
			</view>
			
		</u-card>
		<!-- <view>
			<u-picker mode="time" v-model="show2" :default-time="createDate"  @confirm="confirm2"></u-picker>
		</view> -->
		<view>
			<u-calendar v-model="show2" :defaultDate="createDate" mode="date" @change="confirm2" ></u-calendar>
		</view>
		
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	import scan from "@/components/uni-scan/scan.vue"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"
	
	import config from '../../config.js'
	import radio from "@/components/uni-scan/radio.js"
	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				dataList: [],
				keyword:'',
				createDate:this.formatDate(new Date()),
				// keywordflag: false,
				searchdata:{},
				pageNo: 1,
				hasMore: true,
				keywordflag : true,
				show2:false
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			this.refresh()
		},
		
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中'
			})
			if (this.hasMore) {
				this.pageNo++ 
				setTimeout(() => {
					this.getPackList()
					uni.hideLoading()
				}, 500)
			} else {
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '已经到低了哦',
						duration: 2000
					});
				}, 500)
			}
		},
		
		
		onHide() {
		  // 移除所有全局事件监听器
		  uni.$off('scan');
		  uni.$off('scanner');
		  uni.$off('sm');
		  uni.$off('honeywell');
		},
		onShow() {
			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果1：', res.codeStr);
				if(this.keywordflag){
					this.keyword = res.codeStr;
					this.getPackReviewByTrackNumber(this.keyword);
					this.keywordflag = false
				}else{
					this.goodsNumber = res.codeStr
					this.searchGoods(this.goodsNumber)
				}
			});
		
			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果2：', res.code);
				console.log(this.keywordflag)
				if(this.keywordflag){
					this.keyword = res.code;
					this.getPackReviewByTrackNumber(this.keyword);
					// this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果3：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.getPackReviewByTrackNumber(this.keyword);
					this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果4：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.getPackReviewByTrackNumber(this.keyword);
					this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.searchGoods(this.goodsNumber)
				}
			})
		},
		methods: {
			formatDate(date) {
			      const year = date.getFullYear()
			      const month = String(date.getMonth() + 1).padStart(2, '0')
			      const day = String(date.getDate()).padStart(2, '0')
			      return `${year}-${month}-${day}`
			    },
			getT2(){
				this.show2 = true
			},
			confirm2(e) {
				
				console.log(e)
				this.createDate = e.result
			},
			toBatch(e){
				this.$Router.push({ 
					name: 'storage_put_batch',
					params: { storageNumber:e.storageNumber,goodsId:e.goodsId },
				})
			},
			refresh(){
				this.pageNo = 1
				this.hasMore = true
				this.dataList = [];
				uni.showLoading({
					title: '加载中...'
				})
				this.getPackList()
				uni.hideLoading()
			},
			
			getPackList(){
				this.$http.post('/rest/pack/packList',{
				},{
					params: {
						trackNumber: this.keyword,
						createTime:this.createDate,
						page: this.pageNo,
						limit: 10
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if (this.pageNo == 1) {
						this.dataList = res.data.data
					} else {
						this.dataList = this.dataList.concat(res.data.data)
					}
					// this.orderCount = res.data.count
					this.hasMore = this.pageNo * 10 < res.data.count
					
				})
			},
			search(e){
				this.refresh()
			},
			searchGoods(value){
				let itemList = this.searchdata.list;
				for(let item of itemList) {
					if(item.goodsNumber == value || item.goodsNo == value || item.goodsNo.startsWith(value)){
						this.toBatch(item)
					}
				}
			},
			getFocus(item ,list){
				this.searchdata = item;
				this.keywordflag = false;
			},
			getBlur(value){
				console.log(value);
				let itemList = this.searchdata.list;
				for(let item of itemList) {
					if(item.goodsNumber == value || item.goodsNo == value || item.goodsNo.startsWith(value)){
						this.toBatch(item)
					}
				}
			},
			getKeyWordFocus(){
				this.keywordflag = true;
			},
			toPackReview(item){
				console.log(item)
				this.$Router.replace({
					name: 'packReviewDetails',
					params: { 
						trackNumber:item.trackNumber,
						orderType:item.orderType,
						pickNumber:item.pickNumber
					},
				})
			},
			//根据订单号查询订单
			getPackReviewByTrackNumber(trackNumber){
				this.$http.post('/rest/pack/packList',{
				},{
					params: {
						trackNumber: trackNumber,
						page: 1,
						limit: 10
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log("根据订单号查询订单")
					console.log(res)
					if(res.data.count == 0){
						this.$refs.uToast.show({
							title: '快递单号不存在，请检查快递单号！',
							type: 'error'
						})
						// radio.play_ding_fail();
						// radio.play_ding_duole();
						
						radio.play_ding_cuole();
						// const innerAudioContext = uni.createInnerAudioContext();
						// innerAudioContext.autoplay = true;
						// innerAudioContext.src = config.root +'/system/audio_error';
						// innerAudioContext.onPlay(() => {
						//   console.log('开始播放');
						// });
						// innerAudioContext.onError((res) => {
						//   console.log(res.errMsg);
						//   console.log(res.errCode);
						// });
						
						
						
					}else{
						this.$refs.uToast.show({
							title: '扫码成功~~',
							type: 'success'
						})
						radio.play_ding_success();
						console.log(9999999999999)
						console.log(res)
						this.selectDataList = res.data.data
						this.toPackReview(this.selectDataList[0])
					}
					
				})
			}
		},
	}
</script>